package com.yst.gateway.monitor.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间处理工具类
 * @author sheungxin
 *
 */
public class DateUtils {
	
	public static String getNowTimeStr(){
		return new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(getNowTime());
	}
	
	public static Date getNowTime(){
		return new Date(System.currentTimeMillis());
	}
	
	public static Date getFutureTime(int day,int hour){
		Calendar calendar=Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH)+day);
		calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY)+hour);
		return calendar.getTime();
	}
	
	public static Date addDay(Date date, int num) {
		Calendar startDT = Calendar.getInstance();
		startDT.setTime(date);
		startDT.add(Calendar.DAY_OF_MONTH, num);
		return startDT.getTime();
	}
	
	public static Date formatDate(String dateStr){
		try {
			return new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static Date[] getLastMonthScope(){
		Calendar calendar=Calendar.getInstance();
		Date monthEnd=formatDate(calendar.get(Calendar.YEAR)+"-"+(calendar.get(Calendar.MONTH)+1)+"-"+calendar.get(Calendar.DAY_OF_MONTH)+" 23:59:59");
		calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR)-29);
		Date monthStart=formatDate(calendar.get(Calendar.YEAR)+"-"+(calendar.get(Calendar.MONTH)+1)+"-"+calendar.get(Calendar.DAY_OF_MONTH)+" 00:00:00");
		return new Date[]{monthStart,monthEnd};
	}
	
	public static Date[] getLastWeekScope(){
		Calendar calendar=Calendar.getInstance();
		Date weekEnd=formatDate(calendar.get(Calendar.YEAR)+"-"+(calendar.get(Calendar.MONTH)+1)+"-"+calendar.get(Calendar.DAY_OF_MONTH)+" 23:59:59");
		calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR)-6);
		Date weekStart=formatDate(calendar.get(Calendar.YEAR)+"-"+(calendar.get(Calendar.MONTH)+1)+"-"+calendar.get(Calendar.DAY_OF_MONTH)+" 00:00:00");
		return new Date[]{weekStart,weekEnd};
	}
	
	public static Date[] getLastDayScope(){
		Calendar calendar=Calendar.getInstance();
		int year=calendar.get(Calendar.YEAR);
		int month=calendar.get(Calendar.MONTH)+1;
		int day=calendar.get(Calendar.DAY_OF_MONTH);
		String monthStr=month<10?("0"+month):month+"";
		return new Date[]{formatDate(year+"-"+monthStr+"-"+day+" 00:00:00"),
				formatDate(year+"-"+monthStr+"-"+day+" 23:59:59")};
	}
	
	public static String format(Date date){
		return new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(date);
	}
}
